//
//  SWClassificationViewController.m
//  sc-cyan-ios
//
//  Created by lvjian01 on 2017/12/15.
//  Copyright © 2017年 greenbuilding. All rights reserved.
//
#define collectionViewSectionHeight 50

#import "SWClassificationViewController.h"
#import "SWClassificationTableViewCell.h"
#import "SWClassificationCollectionViewCell.h"
//#import "SWProductListViewController.h"
#import "SWSearchTableViewController.h"

@interface SWClassificationViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollViewContentWidthConstraint;//scrollview宽度约束
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableToScrollMultiplierConstraint;//左右宽度比例约束
@property (weak, nonatomic) IBOutlet UIView *scrollContentView;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (nonatomic, strong) UIView *sectionHeaderView;




@property (nonatomic, strong) NSArray *dataArray;



@end

@implementation SWClassificationViewController

- (NSArray *)dataArray{
    return @[@"hehehehe",@"haha",@"hengheng",@"heihei",@"QNMLGB"];
}

- (CGFloat)collectionViewWidth{
    return Main_Screen_Width / (1 + self.tableToScrollMultiplierConstraint.multiplier);
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self makeViewStyle];
    
    
    
    
//    首先请求数据保存，或从本地获取数据
//    reload
    [self createCollectionViews];

    
    
    
    [self.tableView reloadData];
    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)makeViewStyle{
//    self.tableView.backgroundColor = [UIColor gb_background_f6f6f6];
}

- (void)createCollectionViews{

    CGFloat collectionViewWidth = [self collectionViewWidth];
    self.scrollViewContentWidthConstraint.constant = collectionViewWidth * self.dataArray.count;
    
    CGFloat x_offset = 0;
    for (int i = 0; i < self.dataArray.count; i++) {
        
        CGRect frame = CGRectMake(x_offset, 0, collectionViewWidth, Main_Screen_Height-kStatusBarHeight-kTopBarHeight-kBottomBarHeight);
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:flowLayout];
        collectionView.tag = 100 + i;
        collectionView.dataSource = self;
        collectionView.delegate = self;
        collectionView.alwaysBounceHorizontal = NO;
        collectionView.alwaysBounceVertical = YES;
        collectionView.backgroundColor = UIColorFromRGB(0xF5F5F5);
        collectionView.showsHorizontalScrollIndicator = NO;
        collectionView.showsVerticalScrollIndicator = NO;
        [self.scrollContentView addSubview:collectionView];
        
        [collectionView registerClass:[SWClassificationCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([SWClassificationCollectionViewCell class])];
        [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([UICollectionReusableView class])];
        
        x_offset += collectionViewWidth;
        [collectionView reloadData];
    }
    [self.scrollContentView layoutIfNeeded];
}

- (void)addHeaderContent{
    UIView *headerContent = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [self collectionViewWidth], collectionViewSectionHeight)];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, collectionViewSectionHeight)];
    titleLabel.font = [UIFont systemFontOfSize:12];
    titleLabel.textColor = UIColorFromRGB(0x808080);
    titleLabel.text = @"全部";
    [headerContent addSubview:titleLabel];

//    没有箭头样式，使用xiayibu样式，修改宽度12->8
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(headerContent.width - 22, 0, 8, collectionViewSectionHeight)];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.image = [UIImage imageNamed:@"xiayibu"];
    [headerContent addSubview:imageView];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(10, collectionViewSectionHeight-1, headerContent.width-20, 1)];
    line.backgroundColor = [UIColor gb_line_d4d4d4];
    [headerContent addSubview:line];
    
    UIButton *showAllButton = [[UIButton alloc] initWithFrame:headerContent.bounds];
    [showAllButton addTarget:self action:@selector(showListView) forControlEvents:UIControlEventTouchUpInside];
    [headerContent addSubview:showAllButton];
    
    self.sectionHeaderView = headerContent;
}

- (void)showListView{
    
    
//    这个方法也许可以不存在，后面跳转的方法只要判断数据就可以了；
//    暂时没有别的方法，等接口出来再改
    
    
    SWSearchTableViewController *searchListVC = [SWSearchTableViewController dd_instanceFromStoryboard];
    
//    传值
    
    
    [self.navigationController showViewController:searchListVC sender:nil];
    
}

#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SWClassificationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SWClassificationTableViewCell class]) forIndexPath:indexPath];
    
    
//    cell.
    
    return cell;
}

#pragma mark -- UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SWClassificationTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell.isSelected) {
        [cell setSelected:!cell.isSelected];
        [self.scrollView setContentOffset:CGPointMake(indexPath.row * [self collectionViewWidth], 0) animated:YES];
    }
    
    
}

#pragma mark -- UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 13;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    SWClassificationCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([SWClassificationCollectionViewCell class]) forIndexPath:indexPath];
    
    cell.title = @"推土机";
    
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:NSStringFromClass([UICollectionReusableView class]) forIndexPath:indexPath];
//        天机header时应该添加一个标识，用以显示全部信息列表
        [self addHeaderContent];
        [headerView addSubview:self.sectionHeaderView];
        
        return headerView;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(collectionView.width * 172 / 460, 30);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(collectionView.width, collectionViewSectionHeight);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 20;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

#pragma mark -- UICollectionViewDelegate
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger groupIndex = collectionView.tag - 100;
    
    
//    选择某个cell后进入筛选页面
    
    [self showListView];
    
}


#pragma mark -- UIScrollViewDelegate
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView.tag != 1000) {
//        只有滑动背景scrollview才能出发
        return;
    }
    NSInteger page = floor((scrollView.contentOffset.x - scrollView.width / 2) / scrollView.width) + 1;
//    NSInteger page = round(scrollView.contentOffset.x / scrollView.width);
    
    if (page < 0) {
        page = 0;
    }else if (page >= self.dataArray.count) {
        page = self.dataArray.count - 1;
    }
    
    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:page inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
}
@end
